﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKBatchPrinter.Utils;

namespace YJKBatchPrinter
{
	public class ViewSheets : ISettingNameOperation
	{
		public ViewSheets(Document doc)
		{
			this.m_doc = doc;
			this.m_viewSheetSetting = doc.PrintManager.ViewSheetSetting;
		}

		public string SettingName
		{
			get
			{
				IViewSheetSet currentViewSheetSet = this.m_viewSheetSetting.CurrentViewSheetSet;
				if (!(currentViewSheetSet is ViewSheetSet))
				{
					return "<在任务中>";
				}
				return (currentViewSheetSet as Autodesk.Revit.DB.ViewSheetSet).Name;
			}
			set
			{
				if (value == "<在任务中>")
				{
					this.m_viewSheetSetting.CurrentViewSheetSet = this.m_viewSheetSetting.InSession;
					return;
				}
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
				filteredElementCollector.OfClass(typeof(ViewSheetSet));
				IEnumerable<ViewSheetSet> source = from Autodesk.Revit.DB.ViewSheetSet viewSheetSet in filteredElementCollector
				where viewSheetSet.Name.Equals(value)
				select viewSheetSet;
				if (source.Count<ViewSheetSet>() > 0)
				{
					this.m_viewSheetSetting.CurrentViewSheetSet = source.First<ViewSheetSet>();
				}
			}
		}

		public string Prefix
		{
			get
			{
				return "集 ";
			}
		}

		public int SettingCount
		{
			get
			{
				return new FilteredElementCollector(this.m_doc).OfClass(typeof(ViewSheetSet)).ToElementIds().Count;
			}
		}

		public bool SaveAs(string newName)
		{
			bool result;
			try
			{
				result = this.m_viewSheetSetting.SaveAs(newName);
			}
			catch (Exception ex)
			{
				PrintMgr.MyMessageBox(ex.Message);
				result = false;
			}
			return result;
		}

		public bool Rename(string name)
		{
			bool result;
			try
			{
				result = this.m_viewSheetSetting.Rename(name);
			}
			catch (Exception ex)
			{
				PrintMgr.MyMessageBox(ex.Message);
				result = false;
			}
			return result;
		}

		public List<string> ViewSheetSetNames
		{
			get
			{
				List<string> list = new List<string>();
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
				filteredElementCollector.OfClass(typeof(ViewSheetSet));
				foreach (Element element in filteredElementCollector)
				{
					ViewSheetSet viewSheetSet = element as Autodesk.Revit.DB.ViewSheetSet;
					list.Add(viewSheetSet.Name);
				}
				list.Add("<在任务中>");
				return list;
			}
		}

		public bool Save()
		{
			bool result;
			try
			{
				result = this.m_viewSheetSetting.Save();
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public void Revert()
		{
			try
			{
				this.m_viewSheetSetting.Revert();
			}
			catch (Exception ex)
			{
				PrintMgr.MyMessageBox(ex.Message);
			}
		}

		public bool Delete()
		{
			bool result;
			try
			{
				bool flag = this.m_viewSheetSetting.Delete();
				result = flag;
			}
			catch (Exception ex)
			{
				PrintMgr.MyMessageBox(ex.Message);
				result = false;
			}
			return result;
		}

		public List<Autodesk.Revit.DB.View> AvailableViewSheetSet(VisibleType visibleType)
		{
			if (visibleType == VisibleType.VT_None)
			{
				return null;
			}
			List<Autodesk.Revit.DB.View> list = new List<Autodesk.Revit.DB.View>();
			foreach (object obj in this.m_viewSheetSetting.AvailableViews)
			{
                Autodesk.Revit.DB.View view = (Autodesk.Revit.DB.View)obj;
                if (((int)view.ViewType != 6 || visibleType != VisibleType.VT_ViewOnly) && ((int)view.ViewType == 6 || visibleType != VisibleType.VT_SheetOnly))
				{
					list.Add(view);
				}
			}
			return list;
		}

		public bool IsSelected(string viewName)
		{
			foreach (object obj in this.m_viewSheetSetting.CurrentViewSheetSet.Views)
			{
				View view = (View)obj;
				if (viewName.Equals(RevitViewExtension.GetViewTitle(view)))
				{
					return true;
				}
			}
			return false;
		}

		public void ChangeCurrentViewSheetSet(List<string> names)
		{
			ViewSet viewSet = new ViewSet();
			if (names != null && 0 < names.Count)
			{
				foreach (object obj in this.m_viewSheetSetting.AvailableViews)
				{
					View view = (View)obj;
					if (names.Contains(RevitViewExtension.GetViewTitle(view)))
					{
						viewSet.Insert(view);
					}
				}
			}
			if (viewSet.Size > 0)
			{
				IViewSheetSet currentViewSheetSet = this.m_viewSheetSetting.CurrentViewSheetSet;
				currentViewSheetSet.Views = viewSet;
				this.Save();
			}
		}

		public void ChangeCurrentViewSheetSet(ViewSet newSet)
		{
			if (newSet != null)
			{
				IViewSheetSet currentViewSheetSet = this.m_viewSheetSetting.CurrentViewSheetSet;
				currentViewSheetSet.Views = newSet;
				this.Save();
			}
		}

		public void ClearViewSheetSetViews(string sViewSheetSetName)
		{
			if (!string.IsNullOrEmpty(sViewSheetSetName))
			{
				ViewSheetSet viewSheetSetByName = ViewSheetManager.GetViewSheetSetByName(sViewSheetSetName, this.m_doc);
				if (viewSheetSetByName != null)
				{
					this.m_viewSheetSetting.CurrentViewSheetSet = viewSheetSetByName;
					ViewSet views = new ViewSet();
					IViewSheetSet currentViewSheetSet = this.m_viewSheetSetting.CurrentViewSheetSet;
					currentViewSheetSet.Views = views;
					this.Save();
				}
			}
		}

		private Document m_doc;

		private ViewSheetSetting m_viewSheetSetting;
	}
}
